# import mediapipe as mp
# import cv2
#
#
# class Hands:
#     def __init__(self):
#        self.mp_hands = mp.solutions.hands.Hands()
#        pass
#     def process(self, frame):
#         # mp_hands = mp.solutions.hands.Hands()
#         # 将frame的GRB图像转换为RGB图像
#         mp_hands = self.mp_hands
#         frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#         result = mp_hands.process(frame_rgb)
#         hand_landmarks_list = result.multi_hand_landmarks
#         landmarks = hand_landmarks_list[0]
#         conns = mp.solutions.hands_connections.HAND_CONNECTIONS
#         hands_style = mp.solutions.drawing_utils.DrawingSpec(color=(0,0,255),thickness=2, circle_radius=2)
#         conns_style = mp.solutions.drawing_utils.DrawingSpec(color=(0,0,0),thickness=2, circle_radius=2)
#
#
#
#         # 进行关键点样式的检测
#
#         mp.solutions.drawing_utils.draw_landmarks(
#             image=frame,
#             landmark_list=landmarks,
#             connections=conns,
#             landmark_drawing_spec=hands_style,
#             connection_drawing_spec=conns_style,
#
#         )
#         pass
# if __name__ == '__main__':
#     mp_hands = Hands()
#     cap = cv2.VideoCapture(0)
#     while cap.isOpened():
#         ret,frame = cap.read()
#         if not ret:
#             break
#         # 进行关键点检测
#         mp_hands.process(frame)
#         cv2.imshow('hands', frame)
#         key = cv2.waitKey(120)
#         if key == 'q':
#             break
#     cap.release()
#     cv2.destroyAllWindows()

import mediapipe as mp
import cv2

# print(dir(multi_hand_landmarks))
#
class Hands:
    def __init__(self):
        # 实例化Hands对象
        self.mp_hands = mp.solutions.hands.Hands()
        print("Hands object created")

    def process(self, frame):
        try:
            # 将frame的BGR图像转化为RGB图像
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            #process是Mediapipe库中Hands模块提供的一个方法
            result = self.mp_hands.process(frame_rgb)
            #multi_hand_landmarks检测所有手部的关键点信息
            multi_hand_landmarks = result.multi_hand_landmarks

            if multi_hand_landmarks:
                for landmarks in multi_hand_landmarks:
                    # 连接点
                    conns = mp.solutions.hands.HAND_CONNECTIONS
                    # 关键点样式
                    hands_style = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 0), thickness=2)
                    # 连线样式
                    conns_style = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 0), thickness=1)
                    # 进行关键点样式的绘制
                    mp.solutions.drawing_utils.draw_landmarks(
                        image=frame,
                        landmark_list=landmarks,
                        connections=conns,
                        landmark_drawing_spec=hands_style,
                        connection_drawing_spec=conns_style,
                    )
                    print("绘制手")
                return True
            else:
                print("没有获取手部信息")
                return False
        #Exception捕获所有异常
        except Exception as e:
            print(f"绘制过程中出现的错误: {e}")
            return False

class Face:
    def __init__(self):
        #实例化Face对象
        self.face = mp.solutions.face_mesh.FaceMesh()
        print("创建Face对象")

    def process(self,frame):
        try:
            #将frame的BGR图像转化为RGB图像
            frame_rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
            #process进行数据处理
            result = self.face.process(frame_rgb)
            multi_face_landmarks = result.multi_face_landmarks

            if multi_face_landmarks:
                for landmarks in multi_face_landmarks:
                    #连接点
                    conns = mp.solutions.face_mesh.FACEMESH_CONTOURS
                    #关键点样式
                    face_style = mp.solutions.drawing_utils.DrawingSpec((0,0,0),2)
                    #连线样式
                    conns_style = mp.solutions.drawing_utils.DrawingSpec((0,0,0),1)
                    #进行关键点绘制
                    mp.solutions.drawing_utils.draw_landmarks(
                        frame,
                        landmarks,
                        conns,
                        face_style,
                        conns_style
                    )
                    print("绘制脸部")
                    return True
            else:
                print("没有获取脸部关键点")
                return False
        except Exception as e:
            print(f"绘制过程中出现的其它错误：{e}")
            return False
if __name__ == '__main__':
    #创建Face类的实例mp_face
    mp_face = Face()
    mp_hands = Hands()
    #打开摄像头
    cap = cv2.VideoCapture(0)
    #检查摄像头是否打开
    if not cap.isOpened():
        print("未打开摄像头")
        exit()
    #判断摄像头是否成功有画面
    while cap.isOpened():
        ret,frame = cap.read()
        if not ret:
            print("未获得画面")
            break
        #进行关键点检测
        try:
            detected1 = mp_face.process(frame)
            detected2 = mp_hands.process(frame)
            if not detected1:
                print("未检测到脸部关键点")
            if not detected2:
                print("未检测到手部关键点")
            cv2.imshow('face_hands',frame)
        except Exception as e:
            print("关键点检测出现其它错误")
        key = cv2.waitKey(1)
        if key == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
